/**
 * Class used to communicate with facebook application
 * @author Marcin Bot
 */

package com;

import java.net.*;
import java.util.*;
import java.util.regex.PatternSyntaxException;


public class FacebookInterface {

	private String url;

	/**
	 * Creates a FacebookInterface object
	 * @param addr - an URL of the server hosting the Facebook App
	 */
	public FacebookInterface(String addr)
	{
		if (!addr.endsWith("/"))
			addr = addr + "/";
		if (!addr.startsWith("http://"))
			addr = "http://"+addr;
		url = addr;
	}
	
	
	/**
	 * Sends a post to the Facebook wall
	 * @param posterID - Facebook ID of a person who is sending a post
	 * @param posteeID - Facebook ID of a person who is receiving a post
	 * @param message - message content. Substrings '<poster>' and '<postee>' will be replaced with poster and postee names respectively
	 * 
	 * @return TRUE if the post was successfully sent, FALSE otherwise
	 */
	public boolean postToWall(String posterID, String posteeID, String message)
	{
		URLConnection connection = null;
		try {
			posterID = URLEncoder.encode(posterID,"UTF-8");
			posteeID = URLEncoder.encode(posteeID,"UTF-8");
			message = URLEncoder.encode(message,"UTF-8");
			connection =  new URL(url+"post_to_feed.php"+"?postee_id="+posteeID+"&poster_id="+posterID+"&message="+message).openConnection();
			Scanner scanner = new Scanner(connection.getInputStream());
			scanner.useDelimiter("\\Z");
			if (scanner.findInLine("victory") != null)
				return true;
		}catch ( Exception ex ) {
		    ex.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Sends a game result to the Facebook wall
	 * @param posterIP - an IP of the sending device
	 * @param result - either 'a' (win), 'b' (draw, 'c' (failure)
	 * 
	 * @return TRUE if the post was successfully sent, FALSE otherwise
	 */
	public boolean postResult(String posterIP, char result)
	{
		URLConnection connection = null;
		try {
			posterIP = URLEncoder.encode(posterIP,"UTF-8");
			//System.out.println(url+"post_to_feed.php"+"?postee_ip="+posterIP+"&poster_ip="+posterIP+"&result="+result);
			connection =  new URL(url+"post_result.php"+"?postee_ip="+posterIP+"&poster_ip="+posterIP+"&result="+result).openConnection();
			Scanner scanner = new Scanner(connection.getInputStream());
			scanner.useDelimiter("\\Z");
			if (scanner.findInLine("victory") != null)
				return true;
		}catch ( Exception ex ) {
		    ex.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Updates the score for the user
	 * @param ip - an IP of the sending device
	 * @param result - either 'a' (win), 'b' (draw, 'c' (failure)
	 * 
	 * @return TRUE if the post was successfully sent, FALSE otherwise
	 */
	public boolean recordResult(String ip, char result)
	{
		URLConnection connection = null;
		try {
			ip = URLEncoder.encode(ip,"UTF-8");
			connection =  new URL(url+"record_result.php?ip="+ip+"&result="+result).openConnection();
			Scanner scanner = new Scanner(connection.getInputStream());
			//System.out.println(connection.getURL());
			scanner.useDelimiter("\\Z");
			if (scanner.findInLine("victory") != null)
				return true;
		}catch ( Exception ex ) {
		    ex.printStackTrace();
		}
		return false;
	}
}
